/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function (nums) {
  if (nums.length == 0) return 0
  nums.sort((a, b) => a - b)
  let res = 0
  let dp = new Array(nums.length).fill(1)
  for (let i = 1; i < nums.length; i++) {
    if (nums[i - 1] == nums[i]) {
      dp[i] = dp[i - 1]
    }
    if (nums[i - 1] + 1 == nums[i]) {
      dp[i] = dp[i - 1] + 1
    }
    else {
      res = Math.max(res, dp[i - 1])
    }

  }
  res = Math.max(res, dp[nums.length - 1])
  return res


};

longestConsecutive([1, 2, 0, 1])